import { join } from 'path'
import { readFile } from 'fs/promises'

// 本地静态图片缓存

export default async function handler(req, res) {
  const { name } = req.query
  const tempArr = name.split('.')
  if (tempArr.length !== 2) {
    tempArr[1] = 'png'
  }
  const imagePath = join(process.cwd(), 'public', 'images', `${tempArr[0]}.${tempArr[1]}`)
  try {
    const imageBuffer = await readFile(imagePath)
    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
    res.setHeader('Content-Type', `image/${tempArr[1]}`)
    res.end(imageBuffer)
  } catch (error) {
    res.status(404).end()
  }
}
